using UnityEngine;
using QFramework;
using System.Collections;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class ShotGun : Gun
	{
        //public override Bullet bulletObject => Bullet;

        public override AudioSource AudioSource => SelfAudioSource;

        public override float AtkTime => atkTime;

		private float atkTime = 0;

        private Vector2 dics;
        bool isDoubleWapen = false;

        public float ShootDuration =>1;
        private float mLastShootTime = 0;
        ShootDuration shootDuration;

        private void Start()
        {
            shootDuration = new ShootDuration().
                WithDuration(DataManager.Instance.shotGunModel.
                ShotGunAtkSpeed.Value);

            SetInputAction(()=> Shooting(dics),
                ()=>ShootDown(dics),
                ()=>ShootUp(dics));


            DataManager.Instance.shotGunModel.IsUlockedDouble.Register(value =>
            {

                isDoubleWapen = value;
                GunsTwo.Show();

            }).UnRegisterWhenGameObjectDestroyed(gameObject);



            RemoveBulletCount();
        }

        private void Update()
        {
            MouseDown();
        }
        public void Shoot(Vector2 position, Vector2 dic)
        {
           
            
            ShootBullet.Instantiate().Position(position).Self(self =>
            {
                var selfchage = self;
                selfchage.BulletMoveDic(dic);
                // selfchage.dics = dic.normalized;
                selfchage.SetBulletSpeed(DataManager.Instance.shotGunModel.ShotGunBulletSpeed.Value);

            }).Show();


            ShootBulletTwo.Instantiate().Position(position).Self(self =>
            {

            }).Show();



            SelfAudioSource.clip = ShootSounds[0];
            SelfAudioSource.Play();
        }


        public override void ShootDown(Vector2 dic)
        {
            base.ShootDown(dic);


            if (shootDuration.CanShoot&&DataManager.Instance.
                shotGunModel.CanFireIndex.Value>0)
            {
                shootDuration.RecoreShootTime();
                BulletPosSet(dic, DataManager.Instance.shotGunModel.
                    ShotGunBulletIndex.Value);

            }
        }


        void BulletPosSet(Vector2 dic,int bulletCounts)
        {
            DataManager.Instance.shotGunModel.CanFireIndex.Value -= 1;
            int bulletIndex = 2;
            int lastIndex = 0;
            var angle = dic.ToAngle();
            var originPOS = transform.Position2D();
            var radius = (ShootBullet.Position2D() - originPOS).magnitude;


            int bulletIndexTwo = 2;
            int lastIndexTwo = 0;
            var angle2 = -dic.ToAngle();
            var originPOS2 = transform.Position2D();
            var radius2 = (ShootBulletTwo.Position2D() - originPOS2).magnitude;

            if (isDoubleWapen)
            {
                for (int i = 0; i < bulletCounts; i++)
                {
                    lastIndex = i;
                    ///第一个为正 第二个为负
                    if (lastIndexTwo % 2 == 0)
                    {
                        var angles = angle2 + (-bulletIndexTwo);
                        var direction1 = angles.AngleToDirection2D();
                        var pos = originPOS2 + radius2 * direction1;

                        this.Shoot(pos, direction1);
                        bulletIndexTwo += 2;
                    }
                    else
                    {
                        var angles = angle2 + bulletIndexTwo;
                        var direction1 = angles.AngleToDirection2D();
                        var pos = originPOS2 + radius2 * direction1;
                        this.Shoot(pos, direction1);
                        bulletIndexTwo -= 2;
                    }
                }
            }

            for (int i = 0; i < bulletCounts; i++) 
            {
                lastIndex = i;
                ///第一个为正 第二个为负
                if (lastIndex % 2 == 0)
                {
                    var angles = angle + (-bulletIndex);
                    var direction1 = angles.AngleToDirection2D();
                    var pos = originPOS + radius * direction1;

                    this.Shoot(pos, direction1);
                    bulletIndex += 2;
                }
                else
                {
                    var angles = angle + bulletIndex;
                    var direction1 = angles.AngleToDirection2D();
                    var pos = originPOS + radius * direction1;
                    this.Shoot(pos, direction1);
                    bulletIndex -= 2;
                }
            }
        }

        public override void Shooting(Vector2 dic)
        {
            base.Shooting(dic);
           

            ShootDown(dic);
        }

        public override void ShootUp(Vector2 dic)
        {
            base.ShootUp(dic);
        }

        public override void GetDic(Vector2 dic)
        {
            base.GetDic(dic);

            dics = dic;
           
        }

        /// <summary>
        /// 换弹
        /// </summary>
        public override void RemoveBulletCount()
        {
            base.RemoveBulletCount();
            DataManager.Instance.shotGunModel.CanFireIndex.Register(value =>
            {

                if (value<=0)
                {
                    this.Delay(0.3f, () => StartCoroutine(RemoveBullet(DataManager.
                        Instance.shotGunModel.
                        ShotGunRemoveSpeed.Value)));
                    ///换弹
                    
                }

            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        protected override IEnumerator RemoveBullet(float time)
        {
            return base.RemoveBullet(time);

        }

        public override void OnReloadStart()
        {
            base.OnReloadStart();
            if (isDoubleWapen)
            {
                GunsTwo.Hide();
                RemoveAnimTwo.Show();
            }
            Guns.Hide();
            RemoveAnim.Show();

        }

        public override void OnReloadFinished()
        {
            base.OnReloadFinished();
            RemoveAnim.Hide();

            if (isDoubleWapen)
            {
                RemoveAnimTwo.Hide();
                GunsTwo.Show();
            }
            
            DataManager.Instance.shotGunModel.CanFireIndex.Value = 
                DataManager.Instance.shotGunModel.ShotGunBulletIndex.Value;
            Guns.Show();
        }




    }
}
